package org.yejiali.runner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.yejiali.domain.entity.Article;
import org.yejiali.service.ArticleService;
import org.yejiali.util.RedisCache;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author:yejiali
 * @Date:2025/3/5-03-05-10:38
 * @Decsription:org.yejiali.runner
 * @Version:1.0
 */
@Component
public class ViewCountRunner implements CommandLineRunner {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private RedisCache redisCache;

    //将博客的浏览量储存在redis中
    @Override
    public void run(String... args) throws Exception {
        //查询所有文章博客的浏览量
        List<Article> articleList = articleService.list();
        Map<String,Integer> viewCountMap=articleList.stream()
                .collect(Collectors.toMap(article -> article.getId().toString(),article -> article.getViewCount().intValue()));
        redisCache.setCacheMap("article:viewCountMap",viewCountMap);

    }
}
